<?php
class PublicAction extends BaseAction {

	function login() {
		if (empty ($_SESSION[C('USER_AUTH_KEY')])) {
			$this->display();
		} else {
			$this->redirect('Index/index');
		}
	}

	function checklogin() {
		if (empty ($_POST['username'])) {
			$this->error('帐号错误！');
		}
		elseif (empty ($_POST['password'])) {
			$this->error('密码必须！');
		}

		$map = array ('username'=>$_POST['username'],'active'=>1);
		$user = M('User');
		$authInfo = $user->where($map)->find();

		if (empty ($authInfo)) {
			$this->error('账号不存在或者被禁用!');
		} else {
			if ($authInfo['password'] != md5($_POST['password'])) {
				$this->error('账号密码错误!');
			}
			$_SESSION[C('USER_AUTH_KEY')] = $authInfo['id'];
			$_SESSION['uname'] = $_POST['username'];
			$_SESSION['rid'] = $authInfo['rid'];
//			if ($authInfo['username'] == 'admin') {
				$_SESSION[C('ADMIN_AUTH_KEY')] = true;
//			}
			//查找用户角色权限
			$RolePermission = M('RolePermission');
			$rInfo = $RolePermission->where(array('rid'=>$authInfo['rid']))->findAll();
			if(!empty($rInfo)){
				$sqlIn = '(0';
				$arrFunPermission = array();
				foreach($rInfo as $v){
					$fid = $v['fid'];
					$sqlIn.=','.$fid;
					$arrFunPermission[$fid]=array('c'=>$v['c'],'u'=>$v['u'],'d'=>$v['d']);
				}
				$sqlIn .=')';
				$function = M('Function');
				$arrFun = $function->where('id in'.$sqlIn)->order('id')->select();
				$_SESSION['funOption'] = $arrFunPermission;
				$_SESSION['sysFun'] = $arrFun;
			}
			//更新用户登录
			$data = array ('id'=>$authInfo['id'],'visit_time'=>time(),'visit_ip'=>get_client_ip());
			$user->save($data);
			/*---------------操作日志-----START----------*/
			import("@.Util.Slog");
			Slog::getInstance()->write(CRUD_C,$data);
			/*---------------操作日志-----END------------*/
			unset($user,$RolePermission);
			$this->assign('jumpUrl', __APP__ . '/Index/index');
			$this->success('登录成功!');
		}
	}

	function top() {
		if (empty ($_SESSION[C('USER_AUTH_KEY')])) {
			$this->redirect('Public/login');
		} else {
			$info = array('uname'=>$_SESSION['uname'],'logindt'=>date("l ,F j, Y"));
			$this->assign('obj',$info);
			$this->display();
		}
	}
	function left() {
		if (empty ($_SESSION[C('USER_AUTH_KEY')])) {
			$this->redirect('Public/login');
		} else {
			$sysFun = $_SESSION['sysFun'];
			if(!empty($sysFun)){
				$host= new HostModel();
				$hList =$host->field('id,name')->findAll();
				$hostId = $_POST['hid'];
				if($hostId>0){
					$hostInfo = $host->getById($hostId);
					$_SESSION[C('SELECT_HOST_KEY')] = $hostInfo;
					$this->assign('curHid',$hostId);
				}else{
					$_SESSION[C('SELECT_HOST_KEY')] = null;
				}
				
				$this->assign('hosts',$hList);
				$this->assign('list',$sysFun);
			}
			$this->display();
		}
	}
	function footer() {
		if (empty ($_SESSION[C('USER_AUTH_KEY')])) {
			$this->redirect('Public/login');
		} else {
			$this->display();
		}
	}
	function main() {
		if (empty ($_SESSION[C('USER_AUTH_KEY')])) {
			$this->redirect('Public/login');
		} else {
			$this->display();
		}
	}
	
	function logout() {
		if (!empty ($_SESSION[C('USER_AUTH_KEY')])) {
			/*---------------操作日志-----START----------*/
			import("@.Util.Slog");
			Slog::getInstance()->write(CRUD_D);
			/*---------------操作日志-----END------------*/
			unset ($_SESSION[C('USER_AUTH_KEY')]);
			$_SESSION = array ();
			session_destroy();
			$this->assign('jumpUrl', __URL__ . '/login');
			$this->success('登出成功');
		} else {
			$this->error('已经登出了');
		}
	}
}
?>